function requestInterceptor(request) {
  let params = request.params
  if (params) {
    let entries = []
    for (let key in params) {
      entries.push(`${key}=${params[key]}`)
    }

    let queryString = entries.join('&')
    queryString = encodeURI(queryString)
    delete request.params
    request.url = `${request.url}?${queryString}`
  }

  return request
}

function responseInterceptor() {
  return [
    response => response.data,
    err => {
      const { response } = err
      return Promise.reject({
        status: response?.status || 500,
        data: response?.data
      })
    }
  ]
}

export default {
  requestInterceptor,
  responseInterceptor
}